﻿// window
(function() {

	$gui.window = {
		SHOWN:				0x00000001,
		HIDDEN:				0x00000002,
		CLOSED: 		  	0x00000000,
		CLOSING:		  	0x00000100,
		OPENING: 		 	0x00000200,
		HIDING:   			0x00000400,
		MINIMIZING: 		0x00000800,
		MAXIMIZING: 		0x00001000,
		SHOWING:    		0x00002000,
		RESTORING:  		0x00004000, //after a maximized or minimized
		MINIMIZED:			0x01000000,
		MAXIMIZED:			0x02000000,
		NORMAL:				0x04000000,
		ALWAYSFOREGROUND:	0x00010000	
	};

	$(window).data('gui').plugins['window'] =
	{

		makeHTML : function(param)
		{
			var options = param.options;
			var xml = param.xml;
			var html = param.parent;
		
			var fenetre = document.createElement('div');
			$(fenetre).data('reserved',{guid: '_'+new Date().getTime(), _action : null, _dragOldX : 0, _dragOldY : 0, _maximizeOriginalHeight: null, _minimizeOriginalHeight: null, state: $gui.window.CLOSED});

			
			
			options.container = '';
		
			fenetre.setAttribute('data-effect','window');

			$(fenetre).css('zIndex',$(window).data('gui').zindex++);

			// transform to $gui object
			// dont remove, it's a patch
			fenetre.setAttribute('data-type','window');
			var gfen = $gui(fenetre);

			if (options.notitlebar === undefined)
			{

					// titlebar
				var t = document.createElement("div");
				t.setAttribute('data-effect','titlebar');
				t.setAttribute('style','width : auto');
				t.setAttribute('id','__titlebar__');

					// inline
				var inline = t; //document.createElement("div");
				//inline.setAttribute('data-type', 'inline');
				//inline.setAttribute('style','margin: 0px 0px 0px 0px !important;');
				//t.appendChild(inline);

				if (options.noclose === undefined)
				{
					var close = document.createElement("div");
					close.setAttribute('data-effect', 'titlebar_close');
					close.setAttribute('id','__titlebarclose__');
					inline.appendChild(close);

					$(close).on('click',gfen,function(event) {
						event.data.close();
					});

					$(close).on('dblclick', function(event) {
						event.stopImmediatePropagation();
					});
				}

				if (options.nomaximize === undefined)
				{
					var maximize = document.createElement("div");
					maximize.setAttribute('data-effect', 'titlebar_maximize');
					maximize.setAttribute('id','__titlebarmaximize__');
					inline.appendChild(maximize);

					$(maximize).on('click',gfen, function(event) {
						event.data.maximize(!event.data.isStateEqual($gui.window.MAXIMIZED));
					});

					$(maximize).on('dblclick', function(event) {
						event.stopImmediatePropagation();
					});

					$(t).on('dblclick',gfen, function(event) {
						event.stopPropagation();
						event.data.maximize(!event.data.isStateEqual($gui.window.MAXIMIZED));
					});
				}


				var minimize = document.createElement("div");
				minimize.setAttribute('data-effect', 'titlebar_minimize');
				minimize.setAttribute('id','__titlebarminimize__');
				inline.appendChild(minimize);

				/*
				var outils = document.createElement("div");
				outils.setAttribute('data-effect', 'titlebar_tools');
				outils.setAttribute('id','__titlebartools__');
				inline.appendChild(outils);

				$(outils).on('click',gfen,function(event) {
					//event.data.close();
				});

				$(outils).on('dblclick', function(event) {
					event.stopImmediatePropagation();
				});
				*/
				
				
				var caption = document.createElement("div");
				caption.setAttribute('data-effect', 'titlebar_caption');
				caption.setAttribute('id','__titlebarcaption__');
				inline.appendChild(caption);

				$(caption).data('reserved',{_action : null, data : undefined});
				$(caption).on('webkitAnimationEnd animationend mozAnimationEnd oanimationend MSAnimationEnd', function(event) {
					$(this).removeClass(event.originalEvent.animationName);
					$(this).data('reserved')._action($(this));
					event.stopPropagation();
					if ($(this).data('reserved') !== undefined) $(this).data('reserved')._action = null;
				});

				var titlefen = xml.getAttribute('data-title');
				if (titlefen) $(caption).text(titlefen);

				if (options.nomaximize === undefined)
				{
					$(caption).on('dblclick',gfen,function(event) {
						event.stopPropagation();
						event.data.maximize(!event.data.isStateEqual($gui.window.MAXIMIZED));
					});
				}


				//fenetre.prepend(t);
				fenetre.appendChild(t);

				$(minimize).on('click',gfen,function(event) {
					event.data.minimize(!event.data.isStateEqual($gui.window.MINIMIZED));
				});

				$(minimize).on('dblclick', function(event) {
					event.stopPropagation();
				});

				if (options.fixed === undefined)
				{
					function setPos(e) {
						var x = e.pageX - e.data.data('reserved')._dragOldX;
						var y = e.pageY - e.data.data('reserved')._dragOldY;
						e.data.data('reserved')._dragOldY = e.pageY;
						e.data.data('reserved')._dragOldX = e.pageX;
						e.data.css({ top: e.data.position().top + y, left: e.data.position().left +x});
					}

					$(t).on('mousedown',gfen, function(e) {
						if (e.data.isStateEqual($gui.window.MAXIMIZED)) return false;

						e.data.jquery.data('reserved')._dragOldY = e.pageY;
						e.data.jquery.data('reserved')._dragOldX = e.pageX;

						$(window).on('mousemove',e.data.jquery,setPos);

						$(window).on('mouseup',e.data.jquery,function() {
							$(window).off('mousemove');
							$(window).off('mouseup');
						});
					});

					$(t).on('click', function() {
						$(window).off('mousemove');
					});
				}
			}

			var body = document.createElement("div");
			fenetre.appendChild(body);

			guiTareNameSpace.executeChildrensPlugin(xml,body);
			
			$(fenetre).on('mousedown',gfen, function(event) {
				event.data.foreground();
			});

			//$(fenetre).attr('onsubmit','return false;');
			
			$(fenetre).on('webkitAnimationEnd animationend mozAnimationEnd oanimationend MSAnimationEnd', function(event) {
				$(this).removeClass(event.originalEvent.animationName);
				$(this).removeClass('animated-tiny');
				$(this).data('reserved')._action($(this));
				if ($(this).data('reserved') !== undefined) $(this).data('reserved')._action = null;
			});

			$(fenetre).data('reserved').state = $gui.window.OPENING;
			html.appendChild(fenetre);
			return  fenetre;
		},

		makeXML : function(xml)
		{
			var element = xml.ownerDocument.createElement("window");
			var title = this.title();
			if (title) this[0].setAttribute('data-title',title);
			return element;
		},

		isStateEqual : function(flag)
		{
			return ((this.jquery.data('reserved').state & flag) == flag);
		},

		maximize : function(flag)
		{
			if (flag === true)
			{
				this.jquery.data('reserved').state = (this.jquery.data('reserved').state & 0x00ff00ff) | $gui.window.MAXIMIZING;
				guiTareNameSpace.pushanimation(this.jquery,$(window).data('gui').reserved_maximizeanimationname,
					function(e) {
						e.data('reserved')._maximizeOriginalHeight = e.outerHeight();
						e.data('reserved')._maximizeOriginalWidth = e.outerWidth();
						e.data('reserved')._maximizeOriginalTop = e.position().top;
						e.data('reserved')._maximizeOriginalLeft = e.position().left;
						e.css({
							top : '0px',
							left : '0px',
							height : $(window).height() + "px",
							width : $(window).width() + "px"
						});
						e.data('reserved').state = (e.data('reserved').state & 0x00ff00ff) | $gui.window.MAXIMIZED;
						var onmaximize = e.attr('onmaximize');
						e.off('onmaximize');
						if (onmaximize) e.on('onmaximize', new Function(onmaximize));
						e.trigger("onmaximize");
					}
				);
			} else if (flag === false)
			{
				this.jquery.data('reserved').state = (this.jquery.data('reserved').state & 0x00ff00ff) | $gui.window.RESTORING;
				guiTareNameSpace.pushanimation(this.jquery,$(window).data('gui').reserved_unmaximizeanimationname,
					function(e) {
						e.css({
							top : e.data('reserved')._maximizeOriginalTop + 'px',
							left : e.data('reserved')._maximizeOriginalLeft + 'px',
							height : e.data('reserved')._maximizeOriginalHeight + 'px',
							width : e.data('reserved')._maximizeOriginalWidth + 'px'
						});
						e.data('reserved')._maximizeOriginalHeight = null;
						e.data('reserved')._maximizeOriginalWidth = null;
						e.data('reserved').state = (e.data('reserved').state & 0x00ff00ff) | $gui.window.NORMAL;
					}
				);
			} else if (flag === undefined)
			{
				return this.isStateEqual($gui.window.MAXIMIZED);
			}
			return this;
		},

		minimize : function(flag)
		{
			if (flag === true)
			{
				this.jquery.data('reserved').state = (this.jquery.data('reserved').state & 0x00ff00ff) | $gui.window.MINIMIZING;
				guiTareNameSpace.pushanimation(this.jquery,$(window).data('gui').reserved_minimizeanimationname,
					function(e) {
						var t = e.children('#__titlebar__');
						if (t.length != 0)
						{
							e.data('reserved').state = (e.data('reserved').state & 0x00ff00ff) | $gui.window.MINIMIZED;

							e.data('reserved')._minimizeOriginalHeight = e.outerHeight();
							e.data('reserved')._minimizeOriginalWidth = e.outerWidth();
							e.css('height', ( e.innerHeight() - e.height() + $(t).outerHeight(true) + $(t).position().top) +'px');
							
							var body = e.children('#__titlebar__+div');
							if (body.length != 0)
							{
								body.css('visibility','hidden');
							}
							
							var onminimize = e.attr('onminimize');
							e.off('onminimize');
							if (onminimize) e.on('onminimize', new Function(onminimize));
							e.trigger("onminimize");
						}
					}
				);
			} else if (flag === false)
			{
				this.jquery.data('reserved').state = (this.jquery.data('reserved').state & 0x00ff00ff) | $gui.window.RESTORING;
				guiTareNameSpace.pushanimation(this.jquery,$(window).data('gui').reserved_unminimizeanimationname,
					function(e) {
						//debugger;
						e.css({
							height : e.data('reserved')._minimizeOriginalHeight + 'px',
							width : e.data('reserved')._minimizeOriginalWidth + 'px'
						});
						
						var body = e.children('#__titlebar__+div');
						if (body.length != 0)
						{
							body.css('visibility','visible');
						}

						e.data('reserved')._minimizeOriginalHeight = null;
						e.data('reserved')._minimizeOriginalWidth = null;
						e.data('reserved').state = (e.data('reserved').state & 0x00ff00ff) | $gui.window.NORMAL;
					}
				);
			} else if (flag === undefined)
			{
				return this.isStateEqual($gui.window.MINIMIZED);
			}
			return this;
		},
	  
		close : function()
		{
			if (this.checkValidity()) return false;
			
		  
			$('#modal-'+this.jquery.data('reserved').guid).remove();
			this.jquery.data('reserved').state = (this.jquery.data('reserved').state & 0x00ff00ff) | $gui.window.CLOSING;
			guiTareNameSpace.pushanimation(this.jquery,$(window).data('gui').reserved_closeanimationname,
				function(e) {
					e.data('reserved').state = $gui.window.CLOSED;
					var onunloadevent = e.attr('onunload');
					e.off('onunload');
					if (onunloadevent) e.on('onunload', new Function(onunloadevent));
					e.trigger("onunload");
					e.remove();
				}
			);
			return this;
		},

		show : function(flag)
		{
			if (flag === true)
			{
				$('#modal-'+this.jquery.data('reserved').guid).css('visibility','visible');
				if (!this.isStateEqual($gui.window.HIDDEN)) return this;
				this.jquery.data('reserved').state = (this.jquery.data('reserved').state & 0xffff0000) | $gui.window.SHOWING;
				this.jquery.css('visibility','visible');
				guiTareNameSpace.pushanimation(this.jquery,$(window).data('gui').reserved_showanimationname,
					function(e) {
						e.data('reserved').state = (e.data('reserved').state & 0xffff0000) | $gui.window.SHOWN;
						var onshow = e.attr("onshow");
						e.off('onshow');
						if (onshow) e.on('onshow', new Function(onshow));
						e.trigger("onshow");
					}
				);
			} else if (flag === false)
			{
				$('#modal-'+this.jquery.data('reserved').guid).css('visibility','hidden');
				if (!this.isStateEqual($gui.window.SHOWN)) return this;
				this.jquery.data('reserved').state = (this.jquery.data('reserved').state & 0xffff0000) | $gui.window.HIDING;
				guiTareNameSpace.pushanimation(this.jquery,$(window).data('gui').reserved_hideanimationname,
					function(e) {
						e.css('visibility','hidden');
						e.data('reserved').state = (e.data('reserved').state & 0xffff0000) | $gui.window.HIDDEN;
						var onhide = e.attr("onhide");
						e.off("onhide");
						if (onhide) e.on("onhide", new Function(onhide));
						e.trigger("onhide");
					}
				);
			} else if (flag === undefined)
			{
				return this.isStateEqual($gui.window.SHOWN);
			}
			return this;
		},

		title : function(name)
		{
			var t =  this.jquery.find('#__titlebarcaption__');
			if (t.length != 0)
			{
				if (name === undefined) {
					return t.text();
				}
				this[0].setAttribute('data-title',name);
				guiTareNameSpace.pushanimation(t,$(window).data('gui').reserved_titlechangeanimationname,
					function(e) {
						e.text(name);
					}
				);
				return this;
			}
			return null;
		},

		foreground : function()
		{
			if (this.isStateEqual($gui.window.ALWAYSFOREGROUND)) return this;
			this.jquery.css('z-index', $(window).data('gui').zindex++);
			return this;
		},

		alwaysInForeground : function()
		{
			
			this.jquery.data('reserved').state |= $gui.window.ALWAYSFOREGROUND;
			this.jquery.css('z-index', 90000000);
			return this;
		},
	  
		checkValidity : function()
		{
			var required = this.jquery.find('[required]');
			for (var i = 0; i < required.length;i++)
			{
				var val = required.get(i).checkValidity();
				if (val == false) {
					//this[0].submit();
					return true;
				}
			}
			return false;
		}
	}
})();